﻿using CommonLibrary;
using CommonLibrary.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace CommonSite.Common
{
    public class TemplateHelper
    {
        public static string AlertAndRedirect(string msg, string url)
        {
            return string.Format(
@"<script type=""text/javascript"">alert('{0}'); location.href='{1}';</script>",
                        msg, url);
        }

        public enum Control
        {
            textbox = 1,
            textarea = 2,
            date = 3,
            select = 4,
            digits = 5,
            number = 6,
            hidden = 7
        }

        public static HtmlString CreateControl(wf_FieldInstance field)
        {
            /*
                Control:
                    1;"文本框";"textbox"
                    2;"多行文本框";"textarea"
                    3;"日期控件";"date"
                    4;"下拉框";"select"
                    5;"整数输入框";"digits"
                    6;"浮点数输入框";"number"
                    7;"隐藏控件";"hidden"
            */
            string template = @"<input name=""{0}"" lwidth=""300"" type=""text"" id=""{0}"" ltype=""{1}"" {2} />";

            switch (field.ControlId)
            {
                case 1:
                    return new HtmlString(
                    string.Format(
                        template,
                        "field_" + field.Id,
                        ((Control)field.ControlId).ToString(),
                        field.Nullable ? @"ligerui=""width:300""" : @"ligerui=""width:300"" validate=""{required:true}"""));

                case 3:
                case 5:
                case 6:
                case 7:
                    return new HtmlString(
                        string.Format(
                            template,
                            "field_" + field.Id,
                            ((Control)field.ControlId).ToString(),
                            field.Nullable ? "" : @"validate=""{required:true}"""));

                case 2:
                    return new HtmlString(
                        string.Format(
                            @"<textarea name=""field_{0}"" ltype=""textarea"" {1}></textarea>",
                            field.Id,
                            field.Nullable ? "" : @"validate=""{required:true}"""));

                case 4:
                    return CreateCombobox(field);

                case 10:
                    return CreateTable(field);

                case 8:
                    return new HtmlString(
                        string.Format("<label>{0}</label>", field.Argument));

                case 9:
                    return new HtmlString(
                        string.Format(
                            template,
                            "field_" + field.Id,
                            "date",
                            field.Nullable ? "" : @"ligerui=""{showTime:true}"" validate=""{required:true}"""));


                case 11:
                    return new HtmlString(
                        string.Format(@"<input name=""{0}"" type=""file"" id=""{0}"" ltype=""file"" {1} />",
                                      "field_" + field.Id,
                                      field.Nullable ? "" : @"validate=""{required:true}"""));
            }

            return new HtmlString("&nbsp;");
        }

        public static HtmlString CreateCombobox(wf_FieldInstance field)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(
                @"<select name=""{0}"" id=""{0}"" ltype=""select"" {1}>",
                "field_" + field.Id,
                field.Nullable ? "" : @"validate=""{required:true}""");
            builder.Append(@"<option value=""""></option>");

            using (CommonDatabase db = new CommonDatabase())
            {
                var query = from p in db.wf_Dictionary
                            where p.SubGroup == field.Argument && p.State == 1
                            select new
                            {
                                p.Id,
                                p.Value,
                            };
                foreach (var i in query)
                {
                    builder.AppendFormat(@"<option value=""{0}"">{1}</option>", i.Id, i.Value);
                }
            }

            //builder.Append(@"<option value=""1"">测试</option>");
            builder.Append("</select>");
            return new HtmlString(builder.ToString());
        }

        public static HtmlString CreateTable(wf_FieldInstance field)
        {
            return null;
        }

        public static HtmlString FormatValue(wf_FieldInstance field, object value)
        {
            switch (field.ControlId)
            {
                case 3:
                    return new HtmlString(((DateTime)value).ToString("yyyy年MM月dd日"));

                case 4:
                    using (CommonDatabase db = new CommonDatabase())
                    {
                        var d = db.wf_Dictionary.Where(k => k.Id == (int)value)
                                  .FirstOrDefault();
                        if (d != null)
                            return new HtmlString(d.Value);
                        return new HtmlString("");
                    }

                case 6:
                    return new HtmlString(Convert.ToDecimal(value).ToString("N"));

                case 7:
                    return new HtmlString("");

                case 11:
                    if (value == null ||
                        string.IsNullOrWhiteSpace(value.ToString()))
                    {
                        return new HtmlString("无");
                    }

                    return new HtmlString("<a href=\"" + value + "\">下载</a>");
            }

            return new HtmlString(value.ToString());
        }

        public static HtmlString CreateUserDeptControl()
        {
            var user = UserAction.GetCurrentUser();
            var sb = new StringBuilder();
            sb.Append(@"<select name=""dept"">");
            using (var db = new CommonDatabase())
            {
                var query = from p in db.sys_UserOrgPosition
                            where p.UserId == user.UserId
                            select new 
                            {
                                Value = CryptHelper.Encrypt(p.OrgId),
                                Text = p.sys_Org.Name
                            };
                foreach (var item in query)
                {
                    sb.AppendFormat(@"<option value=""{0}"">{1}</option>",
                        item.Value,
                        item.Text);
                }
            }

            sb.Append("</select>");
            return new HtmlString(sb.ToString());
        }

        public static HtmlString CreateAllDeptsControl()
        {
            return new HtmlString("");
        }

        public static HtmlString CreateAllUsersControl()
        {
            return new HtmlString("");
        }

        public static HtmlString CreateSameDeptUsersControl()
        {
            return new HtmlString("");
        }

        public static List<wf_Column> GetTableHeader(object tableId)
        {
            using (var db = new CommonDatabase())
            {
                var column = db.wf_Column.Where(k=>k.TableId==(int)tableId).OrderBy(o=>o.Id).ToList();
                return column;
            }
        }

        public static DataTable GetTableValues(wf_FieldInstance field,int owner)
        {
            using (var db = new CommonDatabase())
            {
                var values = db.SetCommand(
                              string.Format("select * from workflow.field_{0} where owner = @owner", field.Id),
                              db.Parameter("@owner", owner))
                              .ExecuteDataTable();
                return values;
            }
        }
    }
}